@page "/customization/default-theme"

@using System.Reflection
@using System.Text.RegularExpressions

<DocsPage>
    <DocsPageHeader Title="Default Theme" SubTitle="Here's the default theme class with the default values." />
    <DocsPageContent>
        <DocsPageSection>
            <SectionHeader Title="MudTheme" />
            <SectionSubGroups>
                <DocsPageSection>
                    <SectionHeader Title="Palette"/>
                    <MudSimpleTable Class="docs-class-table default-theme" Dense="true" Elevation="0" FixedHeader="true">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Type</th>
                                <th>Default Light</th>
                                <th>Default Dark</th>
                                <th>CSS Variable</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var Row in ApiPalette)
                            {
                                <tr>
                                    <td>@Row.Name</td>
                                    <td>@Row.Type</td>
                                    <td>
                                        <div class="d-inline-flex align-center">
                                            <MudPaper Outlined="true" Height="16px" Width="16px" Class="mr-2" Style="@($"background-color:{Row.Default}")"/>
                                            @Row.Default
                                        </div>
                                        
                                    </td>
                                    <td>
                                        @if (!Row.Default.Equals(Row.Dark))
                                        {
                                            <div class="d-inline-flex align-center">
                                                <MudPaper Outlined="true" Height="16px" Width="16px" Class="mr-2" Style="@($"background-color:{Row.Dark}")"/>
                                                @Row.Dark
                                            </div>
                                        }
                                    </td>
                                    <td>@Row.CSSVariable</td>
                                </tr>
                            }
                        </tbody>
                    </MudSimpleTable>
                </DocsPageSection>
                <DocsPageSection>
                    <SectionHeader Title="Shadows"/>
                    <MudSimpleTable Class="docs-class-table default-theme" Dense="true" Elevation="0" FixedHeader="true">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Default</th>
                            <th>CSS Variable</th>
                            <th>CSS Class</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach (var Row in ApiShadow)
                        {
                            <tr>
                                <td>@Row.Name</td>
                                <td>@Row.Type</td>
                                <td>@Row.Default</td>
                                <td>@Row.CSSVariable</td>
                                <td>@Row.CSSClass</td>
                            </tr>
                        }
                        </tbody>
                    </MudSimpleTable>
                </DocsPageSection>
                <DocsPageSection>
                    <SectionHeader Title="LayoutProperties"/>
                    <MudSimpleTable Class="docs-class-table default-theme" Dense="true" Elevation="0" FixedHeader="true">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Default</th>
                            <th>CSS Variable</th>
                            <th>CSS Class</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach (var Row in ApiLayout)
                        {
                            <tr>
                                <td>@Row.Name</td>
                                <td>@Row.Type</td>
                                <td>@Row.Default</td>
                                <td>@Row.CSSVariable</td>
                                <td>@Row.CSSClass</td>
                            </tr>
                        }
                        </tbody>
                    </MudSimpleTable>
                </DocsPageSection>
                <DocsPageSection>
                    <SectionHeader Title="Typography"/>
                    <MudSimpleTable Class="docs-class-table default-theme" Dense="true" Elevation="0" FixedHeader="true">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Default</th>
                            <th>CSS Variable</th>
                            <th>CSS Class</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach (var Row in ApiTypography)
                        {
                            <tr>
                                @if (Row.IsHeader)
                                {
                                    <th>@Row.Name</th>
                                    <th>@Row.Type</th>
                                    <th>@Row.Default</th>
                                    <th>@Row.CSSVariable</th>
                                    <th>@Row.CSSClass</th>
                                }
                                else
                                {
                                    <td>@Row.Name</td>
                                    <td>@Row.Type</td>
                                    <td>
                                        @if (Row.CSSVariable.Contains("default-family"))
                                        {
                                            string fontFamily = "";
                                            if (mudTheme.Typography.Default.FontFamily is not null)
                                            {
                                                foreach (string font in mudTheme.Typography.Default.FontFamily)
                                                {
                                                    if (fontFamily == "")
                                                    {
                                                        fontFamily += font;
                                                    }
                                                    else
                                                    {
                                                        fontFamily = fontFamily + ", " + font;
                                                    }
                                                }
                                            }
                                            @fontFamily
                                        }
                                        else
                                        {
                                            @Row.Default
                                        }
                                        
                                    </td>
                                    <td>@Row.CSSVariable</td>
                                    <td>@Row.CSSClass</td>
                                }
                            </tr>
                        }
                        </tbody>
                    </MudSimpleTable>
                </DocsPageSection>
                <DocsPageSection>
                    <SectionHeader Title="ZIndex"/>
                    <MudSimpleTable Class="docs-class-table default-theme" Dense="true" Elevation="0" FixedHeader="true">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Type</th>
                            <th>Default</th>
                            <th>CSS Variable</th>
                            <th>CSS Class</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach (var Row in ApiZindex)
                        {
                            <tr>
                                <td>@Row.Name</td>
                                <td>@Row.Type</td>
                                <td>@Row.Default</td>
                                <td>@Row.CSSVariable</td>
                                <td>@Row.CSSClass</td>
                            </tr>
                        }
                        </tbody>
                    </MudSimpleTable>
                </DocsPageSection>
                
            </SectionSubGroups>
        </DocsPageSection>

    </DocsPageContent>
</DocsPage>

@code {
    MudTheme mudTheme = new MudTheme();

    private List<ApiDefaultTheme> ApiPalette = new List<ApiDefaultTheme>();
    private List<ApiDefaultTheme> ApiShadow = new List<ApiDefaultTheme>();
    private List<ApiDefaultTheme> ApiLayout = new List<ApiDefaultTheme>();
    private List<ApiDefaultTheme> ApiTypography= new List<ApiDefaultTheme>();
    private List<ApiDefaultTheme> ApiZindex= new List<ApiDefaultTheme>();

    protected override void OnInitialized()
    {
        LoadPalette();
        LoadShadows();
        LoadLayout();
        LoadTypography();
        LoadZIndexes();
    }

    private void LoadPalette()
    {
#pragma warning disable CS0618
        foreach (var value in typeof(Palette).GetProperties())
#pragma warning restore CS0618
        {
            var newprop = new ApiDefaultTheme()
            {
                Name = value.Name,
                Type = value.PropertyType.ConvertToCSharpSource(),
                Default = value.GetValue(mudTheme.PaletteLight, null),
                Dark = value.GetValue(mudTheme.PaletteDark, null),
                CSSVariable = $"--mud-palette-{GetCssVar(value.Name)}"
            };
            ApiPalette.Add(newprop);
        }
    }

    private void LoadShadows()
    {
        ApiShadow.Add(new ApiDefaultTheme()
        {
            Name = "Elevation",
            Type = "String[]",
            Default = "System.String[]"
        });
        for (int i = 0; i < mudTheme.Shadows.Elevation.Length; i++)
        {
            int a = i;
            ApiShadow.Add(new ApiDefaultTheme()
            {
                Name = $"Elevation[{a}]",
                Type = "String",
                Default = mudTheme.Shadows.Elevation.GetValue(a),
                CSSVariable = $"--mud-elevation-{a}",
                CSSClass = $"mud-elevation-{a}"
            });
        }
    }
    
    private void LoadLayout()
    {
        foreach (var value in typeof(LayoutProperties).GetProperties())
        {
            ApiLayout.Add(new ApiDefaultTheme()
            {
                Name = value.Name,
                Type = value.PropertyType.ConvertToCSharpSource(),
                Default = value.GetValue(mudTheme.LayoutProperties, null),
                CSSVariable = $"--mud-{GetCssVar(value.Name)}"
            });
        }
    }
    
    private void LoadTypography()
    {
        foreach (var _typography in typeof(Typography).GetProperties())
        {
            ApiTypography.Add(new ApiDefaultTheme()
            {
                Name = _typography.Name,
                Type = _typography.PropertyType.ConvertToCSharpSource(),
                IsHeader = true
            });
            foreach (var item in _typography.GetValue(mudTheme.Typography, null).GetType().GetProperties())
            {
                ApiTypography.Add(new ApiDefaultTheme()
                {
                    Name = _typography.GetValue(mudTheme.Typography, null).GetType().GetProperty(item.Name).Name,
                    Type = _typography.GetValue(mudTheme.Typography, null).GetType().GetProperty(item.Name).PropertyType.ConvertToCSharpSource(),
                    Default = _typography.GetValue(mudTheme.Typography, null).GetType().GetProperty(item.Name).GetValue(_typography.GetValue(mudTheme.Typography, null), null),
                    CSSVariable = $"--mud-typography-{_typography.Name.ToLower()}{GetCssVar(_typography.GetValue(mudTheme.Typography, null).GetType().GetProperty(item.Name).Name)}"
                });
            }
        }
    }
    
    private void LoadZIndexes()
    {
        foreach (var value in typeof(ZIndex).GetProperties())
        {
            ApiZindex.Add(new ApiDefaultTheme()
            {
                Name = value.Name,
                Type = value.PropertyType.ConvertToCSharpSource(),
                Default = value.GetValue(mudTheme.ZIndex, null),
                CSSVariable = $"--mud-zindex-{GetCssVar(value.Name)}"
            });
        }
    }

    private string GetCssVar(string value)
    {
        if (value == "AppBar")
        {
            return value.ToLowerInvariant();
        }
        else if (value.Contains("ContrastText"))
        {
            return value.Replace("ContrastText", "-Text").ToLowerInvariant();
        }
        else if (value.Contains("Font"))
        {
            return value.Replace("Font", "-").ToLowerInvariant();
        }
        else if (value.Contains("LineHeight"))
        {
            return value.Replace("LineHeight", "-LineHeight").ToLowerInvariant();
        }
        else if (value.Contains("LetterSpacing"))
        {
            return value.Replace("LetterSpacing", "-LetterSpacing").ToLowerInvariant();
        }
        else if (value.Contains("TextTransform"))
        {
            return value.Replace("TextTransform", "-Text-Transform").ToLowerInvariant();
        }
        else if (value.Contains("BorderRadius"))
        {
            return value.Replace("BorderRadius", "-BorderRadius").ToLowerInvariant();
        }
        else
        {
            return Regex.Replace(value, @"\B[A-Z]", "-$&").ToLowerInvariant();
        }
    }

    private class ApiDefaultTheme
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public object Default { get; set; }
        public object Dark { get; set; }
        public string CSSClass { get; set; }
        public string CSSVariable { get; set; }
        public bool IsHeader { get; set; }
    }
}
